package pers.xx.sbm.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import pers.xx.sbm.dao.BaseMapper;
import pers.xx.sbm.enums.ResultEnum;
import pers.xx.sbm.service.BaseService;
import pers.xx.sbm.vo.ResResult;

import java.util.List;

/**
 * @author ：xiexing
 * @description：基础service实现类
 * @date ：2020/8/12 22:22
 */
public class BaseServiceImpl<T> implements BaseService<T> {

    BaseMapper dao;//不需要注入 只需要在此声明即可


    @Override
    public ResResult deleteByPrimaryKey(Integer id) {
        int result = dao.deleteByPrimaryKey(id);
        return new ResResult(ResultEnum.SUCCESS, getMsg(result));
    }

    @Override
    public ResResult insert(T record) {
        int result = dao.insert(record);
        return new ResResult(ResultEnum.SUCCESS, getMsg(result));
    }

    @Override
    public ResResult insertSelective(T record) {
        int result = dao.insertSelective(record);
        return new ResResult(ResultEnum.SUCCESS, getMsg(result));
    }

    @Override
    public T selectByPrimaryKey(Integer id) {
        return (T) dao.selectByPrimaryKey(id);
    }

    /**
     * @description 获取数据记录
     * @author XieXing
     * @date 2020/10/12 14:20
     */
    @Override
    public ResResult<T> selectSelective(T record) {
        List<T> records = dao.getListSelective(record);
        return new ResResult(ResultEnum.SUCCESS, records);
    }

    @Override
    public ResResult updateByPrimaryKeySelective(T record) {
        int result = dao.updateByPrimaryKeySelective(record);
        return new ResResult(ResultEnum.SUCCESS, getMsg(result));
    }

    @Override
    public ResResult updateByPrimaryKey(T record) {
        int result = dao.updateByPrimaryKey(record);
        return new ResResult(ResultEnum.SUCCESS, getMsg(result));
    }

    /**
     * @description 分页获取数据
     * @author XieXing
     * @date 2020/8/12 22:24
     */
    @Override
    public ResResult listByPage(T t, Integer page, Integer limit) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        PageHelper.startPage(page, limit);//设置数据库分页查询的范围
        List<T> data = dao.getListSelective(t);
        PageInfo<T> pageInfo = new PageInfo<>(data);
        return new ResResult(ResultEnum.SUCCESS, pageInfo);
    }

    /**
     * @description 更新数据状态信息
     * @author XieXing
     * @date 2020/8/24 21:22
     */
    @Override
    public ResResult updateDataStatus(Integer id, Integer status) {
        int result = dao.updateDataStatus(id, status);
        return new ResResult(ResultEnum.SUCCESS, getMsg(result));
    }

    /**
     * @description 根据dao操作结果配置信息
     * @author XieXing
     * @date 2020/8/23 21:49
     */
    public Object getMsg(int result) {
        Object msg = "成功";
        if (result == 0) {
            msg = "失败";
        }
        return msg;
    }
}
